﻿using System;
using System.Diagnostics;
using TciNewsReader_2014_04.Interfaces;
namespace TciNewsReader_2014_04.Infrastructure
{
    /// <summary>
    /// Stub class for a deployable exception handler
    /// </summary>
    public class ExceptionHandler : IExceptionHandler
    {
        private readonly ILogger _logger;
        public ExceptionHandler(ILogger logger)
        {
            _logger = logger;
        }
        public void HandleException(Exception ex, string culprit)
        {
            StackTrace st = new StackTrace();
            var v1 = st.GetFrames();
            if (v1 != null)
            {
                var method = v1[1].GetMethod().Name;
                Type declaringType = v1[1].GetMethod().DeclaringType;
                if (declaringType != null)
                {
                    var name = declaringType.Name;
                    string s = String.Format("Exception (processing {0}):  {1}.{2} => {3}", culprit, name, method, ex.Message);
                    Console.WriteLine(s);
                    _logger.Log(s);
                }
            }
        }
    }
}
